<?php
	class Admin_Controller extends MY_Controller{

		public function __construct(){
			parent::__construct();

			if($this->uri->segment(1) == 'admin' && $this->uri->segment(2) != 'login'){
				$this->_checkLogin();
			}				
		}

		public function login(){
			if($_POST){
				$this->load->model("users");
				$success = $this->users->validar();

				if($success){
					$datos = array(
								'loggedIn'	=> TRUE,
								'username'	=> $success['username']
							 );
					$this->session->set_userdata($datos);
					redirect("admin");
				}
				else{
					$this->_showLogin(TRUE);
				}
			}
			else{
				$this->_showLogin(FALSE);
			}			
		}
		public function logout(){
			$this->session->unset_userdata("loggedIn");
			$this->session->unset_userdata("username");

			redirect("admin");
		}
		private function _checkLogin(){
			if(!$this->session->userdata('loggedIn')){
				redirect("admin/login");
			}
			
		}

		private function _showLogin($error = FALSE){
			$data['error'] = $error;


			$this->load->view("admin/login",$data);
		}


		function _vistaGeneral($pagina,$data = NULL){		
			if($data == NULL){
				$data = array();
			}

			$data['imgPath']	=	$this->imgPath;	

			$this->load->view("admin/templates/header");
			$this->load->view("admin/$pagina",$data);
			$this->load->view("admin/templates/footer");
		}
	}
?>